package com.zhu.gmallpublisher.utils;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhu.gmallpublisher.config.SugarMessageConfig;
import org.apache.commons.lang3.StringUtils;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class TrafficStatsJsonUtil {

   // private final Integer status = 0;   //默认状态码


    public static <T> String selectResultToJson(List<T> resultList, Class<T> tClass)  {

        //结果列表
        ArrayList<Object> chList = new ArrayList<>();
        ArrayList<Object> countList = new ArrayList<>();

        try {
            Field[] fields = tClass.getDeclaredFields();
            for (T object : resultList) {
                for (int i = 0; i < fields.length; i++) {
                    fields[i].setAccessible(true);
                    if(i==0){
                        chList.add(fields[i].get(object));
                    }else {
                        countList.add(fields[i].get(object));
                    }
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            throw new RuntimeException("属性字段有误");
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": [\"\n" +
                StringUtils.join(chList,"\",\"") + "\n" +
                "\"],\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"用户渠道\",\n" +
                "        \"data\":" +
                countList.toString() + "\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";




      //  System.out.println(chList);
      //  System.out.println(countList);

        }
    //折线图数据接口 数据格式返还
    //数据格式  list <bean(hr,1.2.3) ->
    public static <T> String selectVisitorToJson(List<T> resultList,Class<T> tClass,Integer status,String message) {

        if (status == null) {
            status = SugarMessageConfig.SUGAR_DEFAULT_STATUS_NUMBER;
        }
        if(message == null){
            message = SugarMessageConfig.SUGAR_DEFAULT_MESSAGE_STR;
        }
        //int hourCount = resultList.size();
        ArrayList<Object> valueList = new ArrayList<>();
        Field[] declaredFields = null;

        try {
            declaredFields = tClass.getDeclaredFields();
            for (T object : resultList) {
                for (int i = 0; i < declaredFields.length; i++) {
                    declaredFields[i].setAccessible(true);
                    valueList.add(declaredFields[i].get(object));
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
            throw new RuntimeException("data state is error or sql query is false");
        }

        //定义结果json  status  msg   data
        String basicResultJson = "{\"status\" : 0,\"msg\":\"\",\"data\":\"\"}";
        JSONObject jsonObject = JSON.parseObject(basicResultJson);


        //{ cat[],series [{name,data[]},{},{}] }
        ArrayList<Object> categoriesList = new ArrayList<>();
        ArrayList<Object> seriesList = new ArrayList<>();
        for (int i = 1; i <= valueList.size(); i++) {
            if (i % declaredFields.length == 1) {   //hour
                categoriesList.add(valueList.get(i - 1));
            } else {
                // [b c d b c d b c d]
                seriesList.add(valueList.get(i - 1));
            }
        }
        System.out.println(categoriesList);
        System.out.println(seriesList);
        //{ cat[],series [{name,data[]},{},{}] }
        String dataResultJson = "{\"categories\":\"\",\"series\":\"\"}";
        JSONObject dataJsonObject = JSON.parseObject(dataResultJson);
        ArrayList<JSONObject> seriesJsonObjectList = new ArrayList<>();

        for (int i = 1; i <= declaredFields.length-1; i++) {  //1 2 3
            String innerDataResultJson ="{\"name\":\"\",\"data\":\"\"}";
            JSONObject innerDataResultJsonObject = JSON.parseObject(innerDataResultJson);
            ArrayList<Object> dataArray = new ArrayList<>();
            for (int j = 1; j <= seriesList.size(); j++) {  //1 2 3 4 5 6 7 8 9
                if ((j - i) % (declaredFields.length - 1) == 0) {
                    dataArray.add(seriesList.get(j - 1));
                }
            }
            String name = declaredFields[i].getName();
            innerDataResultJsonObject.put("data",dataArray);
            innerDataResultJsonObject.put("name",name);
            seriesJsonObjectList.add(innerDataResultJsonObject);

        }

        dataJsonObject.put("categories",categoriesList);
        dataJsonObject.put("series",seriesJsonObjectList);
     //   System.out.println(dataJsonObject);
     //   System.out.println(seriesJsonObjectList);
        //  String basicResultJson = "{\"stats\" : 0,\"msg\":\"\",\"data\":\"\"}";

        jsonObject.put("status",status);
        jsonObject.put("message",message);
        jsonObject.put("data",dataJsonObject);
        return jsonObject.toJSONString();



        // a b c d a b c d      i /
    }

        //json
        /*
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": [\n" +
                "      \"00\",\n" +
                "      \"01\",\n" +
                "      ...\n" +
                "    ],\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"独立访客数\",\n" +
                "        \"data\": ,\n"+

                "      },\n" +
                "      {\n" +
                "        \"name\": \"页面浏览数\",\n" +
                "        \"data\": [\n" +
                "          0,\n" +
                "          988,\n" +
                "          ...\n" +
                "        ]\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"新访客数\",\n" +
                "        \"data\": [\n" +
                "          0,\n" +
                "          155,\n" +
                "          ...\n" +
                "        ]\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n";
    }


         */






/*
    public static void main(String[] args) throws InstantiationException, IllegalAccessException {

        ArrayList<TrafficVisitorStatsPerHour> trafficVisitorStatsPerHours = new ArrayList<>();
        TrafficVisitorStatsPerHour one = new TrafficVisitorStatsPerHour(1, 1L, 1L, 1L);
        TrafficVisitorStatsPerHour two = new TrafficVisitorStatsPerHour(2, 2L, 2L, 2L);
        TrafficVisitorStatsPerHour three = new TrafficVisitorStatsPerHour(3, 3L, 3L, 3L);
        trafficVisitorStatsPerHours.add(one);
        trafficVisitorStatsPerHours.add(two);
        trafficVisitorStatsPerHours.add(three);
        System.out.println(selectVisitorToJson(trafficVisitorStatsPerHours,TrafficVisitorStatsPerHour.class,0,null));
    }
 */





}
